#include <stdio.h>

#define SIZE 5

// 循环队列
typedef struct {
    // 数据
    char data[SIZE];
    // 头指针，尾指针
    int front, rear;
} Queue;

// 是否满了
int isFull(Queue *queue) {
    return (queue->rear + 1) % SIZE == queue->front;
}

// 入队
void push(Queue *queue, char val) {

    if (isFull(queue)) {
        printf("queue is full \n");
        return;
    }
    int index = queue->rear;

    queue->data[index] = val;
    queue->rear = (index + 1) % SIZE;
}

// 是否为空队列
int isEmpty(Queue *queue) {
    return queue->front == queue->rear;
}


char pop(Queue *queue) {

    if (isEmpty(queue)) {
        printf("queue is empty \n");
        return NULL;
    }

    int index = queue->front;

    char top = queue->data[index];
    queue->front = (index + 1) % SIZE;

    return top;
}

// 初始化一个队列
Queue init() {

    Queue queue;

    queue.front = 0;
    queue.rear = 0;

    return queue;
}

int main() {

    Queue start = init();
    Queue *top = &start;

    push(top, 'a');
    push(top, 'b');
    push(top, 'c');
    push(top, 'd');
    // 这个e 无法被加入进去,因为满了
    push(top, 'e');
    printf("push queue after front=%d,rear=%d \n", start.front, start.rear);

    char a = pop(top);
    char b = pop(top);
    printf("pop char a=%c \n", a);
    printf("pop char b=%c \n", b);

    printf("pop queue after front=%d,rear=%d \n", start.front, start.rear);

    push(top, 'f');
    push(top, 'g');
    push(top, 'h');

    printf("push queue after front=%d,rear=%d \n", start.front, start.rear);

    return 0;
}
